package com.j2eefast.produce.pms.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.Global;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsReviewEntity;
import com.j2eefast.produce.pms.entity.PmsReviewReportEntity;
import com.j2eefast.produce.pms.service.PmsReviewReportService;
import com.j2eefast.produce.pms.service.PmsReviewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * <p>评审报备</p>
 *
 * @author: summer
 * @date: 2021-05-29 22:03
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/pms/reviewReport")
public class PmsReviewReportController extends BaseController {
	private String urlPrefix = "modules/pms/reviewReport";
	@Autowired
	private PmsReviewReportService pmsReviewReportService;
	@Autowired
	private PmsReviewService pmsReviewService;

	private String typeName = "";
	private String pathName="";
	private Long fileSize=0L;


	@RequiresPermissions("pms:reviewReport:view")
	@GetMapping()
	public String process() {


		return urlPrefix + "/index";
	}

	@RequestMapping("/list")
	@RequiresPermissions("pms:reviewReport:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params) {
		PageUtil page = pmsReviewReportService.findPage(params);
		return success(page);
	}




	/**
	 * 删除
	 */
	@BussinessLog(title = "删除", businessType = BusinessType.DELETE)
	@RequestMapping( value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("pms:reviewReport:del")
	@RequiresRoles(Constant.SU_ADMIN)
	@ResponseBody
	public ResponseData delete(Long[] ids) {
		return pmsReviewReportService.deleteByIds(ids)?success(): error("删除失败!");
	}



	/**
	 * 报备
	 */
	@RequiresPermissions("pms:reviewReport:reportList")
	@GetMapping("/reportList/{id}")
	public String reportList(@PathVariable("id") String id,ModelMap mmap) {
//		System.out.println(id);
//		System.out.println("121212121");
//		params.put("id",id);

		//PageUtil page = pmsReviewReportService.findPage(params);

		//System.out.println("3333333333333333");
	//	return success(page);
		PmsReviewEntity pmsReviewEntity= pmsReviewService.getById(id);
		mmap.put("name",pmsReviewEntity.getProjectName());

		return urlPrefix + "/index";
	}
	@RequestMapping("/reportList")
	@RequiresPermissions("pms:reviewReport:reportList")
	@ResponseBody
	public ResponseData reportList(@RequestParam Map<String, Object> params) {
		PageUtil page = pmsReviewReportService.findPage(params);
		return success(page);
	}


	/**
	 * 修改出入库 页面
	 */
	@RequiresPermissions("pms:reviewReport:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		PmsReviewReportEntity pmsReviewReportEntity = pmsReviewReportService.getById(id);
		mmap.put("reviewReport", pmsReviewReportEntity);
		return urlPrefix + "/edit";
	}

	/**
	 * 修改出入库保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:reviewReport:edit")
	@BussinessLog(title = "修改", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData edit(PmsReviewReportEntity pmsReviewReportEntity) {
		ValidatorUtil.validateEntity(pmsReviewReportEntity);
		return pmsReviewReportService.updateById(pmsReviewReportEntity) ? success() : error("修改失败!");
	}

	/**
	 * 新增参数配置
	 */
	@RequiresPermissions("pms:reviewReport:add")
	@GetMapping("/add")
	public String add() {
		return urlPrefix + "/add";
	}



	/**
	 * 新增保存
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:reviewReport:add")
	@BussinessLog(title = "认领", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addSave(@Validated PmsReviewReportEntity pmsReviewReportEntity) {
		//校验参数
		ValidatorUtil.validateEntity(pmsReviewReportEntity);
		String  userName= UserUtils.getUserInfo().getName();
		//SysUserEntity sysUserEntity=pmsProjectService.getSysById(userId);
//		pmsProjectEntity.setRegisterName(sysUserEntity.getName());
		Date date=new Date();
		pmsReviewReportEntity.setCreateTime(date);
		pmsReviewReportEntity.setUpdateTime(date);
		pmsReviewReportEntity.setCreateBy(userName);
		pmsReviewReportEntity.setUpdateBy(userName);
		return pmsReviewReportService.save(pmsReviewReportEntity) ? success() : error("新增失败!");
	}

	/**
	 * 修改出入库保存
	 */

	@RequiresPermissions("pms:reviewReport:checkout")
	@BussinessLog(title = "出库", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/checkout", method = RequestMethod.POST)
	@ResponseBody
//	@GetMapping("/checkout/{id}")
	public ResponseData checkout(String id, ModelMap mmap) {

		ValidatorUtil.validateEntity(id);
		Long ids=Long.parseLong(id);
		return pmsReviewReportService.updateCheck(ids) ? success() : error("修改失败!");
	}

	/**
	 * 上传资料
	 */
	@RepeatSubmit
	@BussinessLog(title = "上传", businessType = BusinessType.DELETE)
	@RequestMapping(value = "/doUploadFile", method = RequestMethod.POST)
	@RequiresPermissions("pms:review:doUploadFile")
	@ResponseBody
	public ResponseData doUploadFile(MultipartFile file) {
		try {
			if (!file.isEmpty()) {
				pathName = FileUploadUtil.uploadWeb(Global.getTempPath(), file);
				typeName = pathName.substring(pathName.indexOf(".")+1);
				System.out.println("上传成功");
				fileSize=file.getSize();
				return success("上传成功",pathName);
			} else {
				return error(ToolUtil.message("sys.file.null"));
			}
		} catch (IOException e) {
			return error("上传失败" + e);
		}
	}






}
